package com.quectel.cms.controller.grid;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.grid.dto.GridMemberGridBindingDto;
import com.quectel.core.module.grid.service.GridMemberGridBindingService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/02/03 15:45
 */
@RestController
@RequestMapping("gridMemberGridBinding")
@Tag(name = "GridMemberGridBindingController", description = "网格员和网格关系绑定表相关api")
public class GridMemberGridBindingController extends BaseController {

    @DubboReference
    private GridMemberGridBindingService gridMemberGridBindingService;

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "gridMemberGridBinding:list")
    @Operation(summary = "网格员和网格关系的分页查询列表")
    public Response<Page<GridMemberGridBindingDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = gridMemberGridBindingService.queryTotal(request.getParams());

        List<GridMemberGridBindingDto> list = null;
        if (total > 0) {
            list = gridMemberGridBindingService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<GridMemberGridBindingDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<GridMemberGridBindingDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "gridMemberGridBinding:edit")
    @Operation(summary = "查询单条网格员和网格关系记录")
    public Response<GridMemberGridBindingDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (gridMemberGridBindingService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        GridMemberGridBindingDto gridMemberGridBindingDto = gridMemberGridBindingService.selectById(id);

        return Response.<GridMemberGridBindingDto>ok().wrap(gridMemberGridBindingDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "gridMemberGridBinding:info")
    @Operation(summary = "查询单条网格员和网格关系记录")
    public Response<GridMemberGridBindingDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (gridMemberGridBindingService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        GridMemberGridBindingDto gridMemberGridBindingDto = gridMemberGridBindingService.selectById(id);

        return Response.<GridMemberGridBindingDto>ok().wrap(gridMemberGridBindingDto);
    }

    /**
     * 保存
     *
     * @param gridMemberGridBindingDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "gridMemberGridBinding:edit")
    @Operation(summary = "保存单条网格员和网格关系记录")
    public Response<Object> save(@RequestBody @Validated GridMemberGridBindingDto gridMemberGridBindingDto) {

        //填充基础数据
        fillSaveCommonData(gridMemberGridBindingDto);

        gridMemberGridBindingService.save(gridMemberGridBindingDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param gridMemberGridBindingDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "gridMemberGridBinding:edit")
    @Operation(summary = "修改单条网格员和网格关系记录")
    public Response<Object> update(@RequestBody @Validated GridMemberGridBindingDto gridMemberGridBindingDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(gridMemberGridBindingDto.getId()));
        if (gridMemberGridBindingService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(gridMemberGridBindingDto);

        gridMemberGridBindingService.updateById(gridMemberGridBindingDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "gridMemberGridBinding:delete")
    @Operation(summary = "批量删除网格员和网格关系记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (gridMemberGridBindingService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        gridMemberGridBindingService.deleteBatch(ids);

        return Response.ok();
    }
}
